import logging
import time

class SMartLogger:
    # ANSI颜色代码
     # ANSI颜色代码
    colors = {
        "DEBUG": "\033[94m",    # 蓝色
        "INFO": "\033[92m",     # 绿色
        "WARNING": "\033[93m",  # 黄色
        "ERROR": "\033[91m",    # 红色
        "CRITICAL": "\033[95m",  # 紫色
        "RESET": "\033[0m"     # 重置颜色
    }
    


    @classmethod
    def info(cls,message,color,level,_file):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        logging.info(f"{color}[{level}] {timestamp}: {message}")
        print(f"{color}[{level}] {timestamp}: {message}",file=_file)

    @classmethod
    def debug(cls,message,color,level,_file):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        logging.debug(f"{color}[{level}] {timestamp}: {message}")
        print(f"{color}[{level}] {timestamp}: {message}",file=_file)


    @classmethod
    def warning(cls,message,color,level,_file):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        #print(f"{color}[{level}] {timestamp}: {message}")
        logging.warning(f"{color}[{level}] {timestamp}: {message}")
        print(f"{color}[{level}] {timestamp}: {message}",file=_file)


    @classmethod
    def error(cls,message,color,level,_file):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        logging.error(f"{color}[{level}] {timestamp}: {message}")
        print(f"{color}[{level}] {timestamp}: {message}",file=_file)

    @classmethod
    def critical(cls,message,color,level,_file):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        logging.critical(f"{color}[{level}] {timestamp}: {message}")
        print(f"{color}[{level}] {timestamp}: {message}",file=_file)

    @classmethod
    def log(cls,message,level="INFO",logFileName="loginfo.txt"):
       # 写入日志文件
       try:
           with open(logFileName,'w',encoding='utf-8') as f:
              print(f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}] - {level}",file=f)
              logColor = cls.colors.get(level)
              if level == "INFO":
                 cls.info(message,logColor,level,f)
              elif level == "DEBUG":
                  cls.debug(message,logColor,level,f)
              elif level == "WARNING":
                  cls.warning(message,logColor,level,f)
              elif level == "ERROR":
                  cls.error(message,logColor,level,f)
              elif level == "CRITICAL":
                  cls.critical(message,logColor,level,f)
       except Exception as e:
           with open("error.log.txt","w",encoding="utf-8") as f:
               print(f"写入日志文件失败: {e}",f)

#SMartLogger.log("这是一条INFO日志","INFO")
#SMartLogger.log("这是一条DEBUG日志","DEBUG")
#SMartLogger.log("这是一条WARNING日志","WARNING")
#SMartLogger.log("这是一条ERROR日志","ERROR")
SMartLogger.log("这是一条CRITICAL日志","CRITICAL")


